package com.bw.day03.login

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.graphics.Color
import android.graphics.Color.*
import android.os.Build
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.net.mvvm.BaseActivity
import com.bw.base.net.net.Cons
import com.bw.day03.MainActivity
import com.bw.day03.R
import com.bw.day03.databinding.ActivityLoginCodeBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@Route(path = "/app/LoginCodeActivity")
class LoginCodeActivity : BaseActivity<ActivityLoginCodeBinding,LoginViewModel>() {
    override fun initView() {
        binding.btnSendcode.setOnClickListener {
           val phone=binding.etPhone.text.toString()
            if (phone.isEmpty()){
                ToastUtils.showLong("手机号不能为空")
                return@setOnClickListener
            }
//            if (!RegexUtils.isMobileExact(phone)){
//
//            }
            if (!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }
            viewModel.sendCode(phone)
            binding.btnSendcode.isEnabled=false
            lifecycleScope.launch {
                for (i in 15 downTo 1){
                    binding.btnSendcode.text="$i 秒后获取"
                    delay(1000)
                    if (i==1){
                        binding.btnSendcode.text="获取验证码"
                        binding.btnSendcode.isEnabled=true
                    }

                }
            }
        }
        binding.btnLogin.setOnClickListener {
            val phone=binding.etPhone.text.toString()
            val code=binding.etCode.text.toString()
            if (phone.isEmpty()||code.isEmpty()){
                ToastUtils.showLong("手机号或验证码不能为空")
                return@setOnClickListener
            }
            if (!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }
            if (!binding.cbFwb.isChecked){
                ToastUtils.showLong("请勾选服务条款")
                return@setOnClickListener
            }

            viewModel.loginCode(code,phone)
        }
        val ssb = SpannableStringBuilder(binding.cbFwb.text.toString())
        ssb.setSpan(ForegroundColorSpan(RED),2,4,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        val clickableSpan=object : ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebViewActivity").withString("url","http://www.baidu.com").navigation()
            }

        }
        val clickableSpan2=object :ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebViewActivity").withString("url","http://www.sina.com").navigation()
            }

        }
        ssb.setSpan(clickableSpan,7,13,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        ssb.setSpan(clickableSpan2,14,20,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.cbFwb.movementMethod= LinkMovementMethod.getInstance()
        binding.cbFwb.text=ssb

    }


    override fun initData() {
       viewModel.sendSuccess.observe(this){
           if (it.code==200){
               binding.etCode.setText(it.data.content.substring(8,14))
               val manager= getSystemService(NOTIFICATION_SERVICE) as NotificationManager
               if (Build.VERSION.SDK_INT> Build.VERSION_CODES.O){
                   val notification=Notification.Builder(this)
                       .setSmallIcon(R.mipmap.ic_launcher)
                       .setContentTitle("验证码")
                       .setContentText("验证码为${it.data.content.substring(8,14)}")
                       .setChannelId("channelId")
                       .build()
                   val notificationChannel=
                       NotificationChannel("channelId","channelName", NotificationManager.IMPORTANCE_DEFAULT)
                   manager.createNotificationChannel(notificationChannel)
                   manager.notify(1,notification)
               }else {
                   val notification = Notification.Builder(this)
                       .setSmallIcon(R.mipmap.ic_launcher)
                       .setContentTitle("验证码")
                       .setContentText("验证码为${it.data.content.substring(8, 14)}")
                       .build()
                   manager.notify(1, notification)
               }
               ToastUtils.showLong("发送成功")
           }
       }
        viewModel.codeSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if (it.code==200){
                SPUtils.getInstance().put(Cons.SP_TOKEN,it.data.token)
                SPUtils.getInstance().put(Cons.SP_PHONE,it.data.phone)
                finish()
                ARouter.getInstance().build("/app/ShopActivity").navigation()
            }
        }
        viewModel.codeFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<LoginViewModel> = LoginViewModel::class.java

    override fun getLayoutId(): Int = R.layout.activity_login_code
}